package com.breeze.components.socket.redis.action;

import com.alibaba.fastjson2.JSONObject;
import com.breeze.components.socket.WebSocket;
import com.breeze.components.socket.WebSocketManager;
import com.breeze.components.socket.util.WebSocketUtil;
import java.util.Objects;

/**
 * { "action":"sendMessage", "identifier":"xxx", "message":"xxxxxxxxxxx" } 给webSocket发送消息的action
 *
 * @author breeze
 */
public class SendMessageAction implements Action {

  @Override
  public void doMessage(WebSocketManager manager, JSONObject object) {
    if (!object.containsKey(IDENTIFIER) || !object.containsKey(MESSAGE)) {
      return;
    }
    String identifier = object.getString(IDENTIFIER);
    WebSocket webSocket = manager.get(identifier);
    if (Objects.isNull(webSocket)) {
      return;
    }
    WebSocketUtil.sendMessage(webSocket.getSession(), object.getString(MESSAGE));
  }
}
